﻿Imports System.IO

Public Class 批量入货

#Region "外观处理"

    '关闭窗体
    Private Sub 入货管理_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        CommoMulIn = Nothing
    End Sub


    '第2行使能
    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked Then
            ComboBox4.Enabled = True
            ComboBox3.Enabled = True
            TextBox6.Enabled = True
            TextBox5.Enabled = True
            TextBox4.Enabled = True
            Label10.Enabled = True
            Label9.Enabled = True
            NumericUpDown2.Enabled = True
        Else
            ComboBox4.Enabled = False
            ComboBox3.Enabled = False
            TextBox6.Enabled = False
            TextBox5.Enabled = False
            TextBox4.Enabled = False
            NumericUpDown2.Enabled = False
            Label10.Enabled = False
            Label9.Enabled = False
        End If
    End Sub

    '第3行使能
    Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
        If CheckBox2.Checked Then
            ComboBox6.Enabled = True
            ComboBox5.Enabled = True
            TextBox9.Enabled = True
            TextBox8.Enabled = True
            TextBox7.Enabled = True
            NumericUpDown3.Enabled = True
            Label12.Enabled = True
            Label11.Enabled = True
        Else
            ComboBox6.Enabled = False
            ComboBox5.Enabled = False
            TextBox9.Enabled = False
            TextBox8.Enabled = False
            TextBox7.Enabled = False
            NumericUpDown3.Enabled = False
            Label12.Enabled = False
            Label11.Enabled = False
        End If
    End Sub

    '第4行使能
    Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
        If CheckBox3.Checked Then
            ComboBox8.Enabled = True
            ComboBox7.Enabled = True
            TextBox12.Enabled = True
            TextBox11.Enabled = True
            TextBox10.Enabled = True
            Label13.Enabled = True
            Label14.Enabled = True
            NumericUpDown4.Enabled = True
        Else
            ComboBox8.Enabled = False
            ComboBox7.Enabled = False
            TextBox12.Enabled = False
            TextBox11.Enabled = False
            TextBox10.Enabled = False
            Label13.Enabled = False
            Label14.Enabled = False
            NumericUpDown4.Enabled = False
        End If
    End Sub

    '第5行使能
    Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
        If CheckBox4.Checked Then
            ComboBox10.Enabled = True
            ComboBox9.Enabled = True
            TextBox15.Enabled = True
            TextBox14.Enabled = True
            TextBox13.Enabled = True
            NumericUpDown5.Enabled = True
            Label15.Enabled = True
            Label16.Enabled = True
        Else
            ComboBox10.Enabled = False
            ComboBox9.Enabled = False
            TextBox15.Enabled = False
            TextBox14.Enabled = False
            TextBox13.Enabled = False
            Label15.Enabled = False
            Label16.Enabled = False
            NumericUpDown5.Enabled = False
        End If
    End Sub

    '第6行使能
    Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
        If CheckBox5.Checked Then
            ComboBox12.Enabled = True
            ComboBox11.Enabled = True
            TextBox18.Enabled = True
            TextBox17.Enabled = True
            TextBox16.Enabled = True
            NumericUpDown6.Enabled = True
            Label17.Enabled = True
            Label18.Enabled = True
        Else
            ComboBox12.Enabled = False
            ComboBox11.Enabled = False
            TextBox18.Enabled = False
            TextBox17.Enabled = False
            TextBox16.Enabled = False
            Label17.Enabled = False
            Label18.Enabled = False
            NumericUpDown6.Enabled = False
        End If
    End Sub

    '第7行使能
    Private Sub CheckBox6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox6.CheckedChanged
        If CheckBox6.Checked Then
            ComboBox14.Enabled = True
            ComboBox13.Enabled = True
            TextBox21.Enabled = True
            TextBox20.Enabled = True
            TextBox19.Enabled = True
            NumericUpDown7.Enabled = True
            Label19.Enabled = True
            Label20.Enabled = True
        Else
            ComboBox14.Enabled = False
            ComboBox13.Enabled = False
            TextBox21.Enabled = False
            TextBox20.Enabled = False
            TextBox19.Enabled = False
            Label19.Enabled = False
            Label20.Enabled = False
            NumericUpDown7.Enabled = False
        End If
    End Sub

#End Region

#Region "输入检测"

    '只能输入数字
    Private Sub NumOnly(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If e.KeyChar >= "0" And e.KeyChar <= "9" Or e.KeyChar = Chr(Keys.Back) Or e.KeyChar = Chr(46) Then  '只允许输入数字与点
            e.Handled = False           '当按键不合法时　则不允许输入
        Else
            e.Handled = True            '当按键合法时　则允许输入并显示
        End If
    End Sub
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        NumOnly(e)
    End Sub
    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        NumOnly(e)
    End Sub
    Private Sub TextBox5_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox5.KeyPress
        NumOnly(e)
    End Sub
    Private Sub TextBox6_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox6.KeyPress
        NumOnly(e)
    End Sub
    Private Sub TextBox8_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox8.KeyPress
        NumOnly(e)
    End Sub
    Private Sub TextBox9_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox9.KeyPress
        NumOnly(e)
    End Sub
    Private Sub TextBox11_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox11.KeyPress
        NumOnly(e)
    End Sub
    Private Sub TextBox12_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox12.KeyPress
        NumOnly(e)
    End Sub
    Private Sub TextBox14_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox14.KeyPress
        NumOnly(e)
    End Sub
    Private Sub TextBox15_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox15.KeyPress
        NumOnly(e)
    End Sub
    Private Sub TextBox17_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox17.KeyPress
        NumOnly(e)
    End Sub
    Private Sub TextBox18_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox18.KeyPress
        NumOnly(e)
    End Sub
    Private Sub TextBox20_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox20.KeyPress
        NumOnly(e)
    End Sub
    Private Sub TextBox21_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox21.KeyPress
        NumOnly(e)
    End Sub

#End Region

#Region "加载型号"

    '加载型号
    Private Sub LoadXH(ByVal combox_a As ComboBox, ByVal combox_b As ComboBox)
        Try
            combox_b.Text = "请选择..."

            If combox_a.Text = "请选择..." Or combox_a.Text = "" Then
                Exit Sub
            End If

            Dim sqlstr As String = "select 型号 from 型号表 where 所属品牌 = '" + combox_a.Text + "'"
            Dim ds As Data.DataSet = SQLtoDataSet(sqlstr)
            combox_b.Items.Clear()
            Dim ds_table As Data.DataTable = ds.Tables(0)
            If ds_table.Rows.Count > 0 Then
                For i = 0 To ds_table.Rows.Count - 1
                    combox_b.Items.Add(ds_table.Rows(i).Item(0).ToString)
                Next
            End If

        Catch ex As Exception

        End Try
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        LoadXH(ComboBox1, ComboBox2)
        ComboBox4.SelectedIndex = ComboBox1.SelectedIndex
        ComboBox6.SelectedIndex = ComboBox1.SelectedIndex
        ComboBox8.SelectedIndex = ComboBox1.SelectedIndex
        ComboBox10.SelectedIndex = ComboBox1.SelectedIndex
        ComboBox12.SelectedIndex = ComboBox1.SelectedIndex
        ComboBox14.SelectedIndex = ComboBox1.SelectedIndex
    End Sub
    Private Sub ComboBox4_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox4.SelectedIndexChanged
        LoadXH(ComboBox4, ComboBox3)
    End Sub
    Private Sub ComboBox6_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox6.SelectedIndexChanged
        LoadXH(ComboBox6, ComboBox5)
    End Sub
    Private Sub ComboBox8_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox8.SelectedIndexChanged
        LoadXH(ComboBox8, ComboBox7)
    End Sub
    Private Sub ComboBox10_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox10.SelectedIndexChanged
        LoadXH(ComboBox10, ComboBox9)
    End Sub
    Private Sub ComboBox12_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox12.SelectedIndexChanged
        LoadXH(ComboBox12, ComboBox11)
    End Sub
    Private Sub ComboBox14_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox14.SelectedIndexChanged
        LoadXH(ComboBox14, ComboBox13)
    End Sub

#End Region

#Region "选择型号"


    '择择型号
    Private Sub LoadPR(ByVal combox_a As ComboBox, ByVal combox_b As ComboBox, ByVal tx As TextBox, ByVal tx2 As TextBox, ByVal nu As NumericUpDown)

        If combox_b.Text = "请选择..." Or combox_b.Text = "" Then
            Exit Sub
        End If

        Dim sqlstr As String = "select 入货单价 from 入货表 where 品牌 = '" + combox_a.Text + "' and 型号 ='" + combox_b.Text + "'"

        Dim ds As Data.DataSet = SQLtoDataSet(sqlstr)
        Dim ds_table As Data.DataTable = ds.Tables(0)
        If ds_table.Rows.Count > 0 Then
            tx.Text = ds_table.Rows(0).Item(0).ToString
            tx2.Text = (Val(tx.Text) * nu.Value).ToString
        Else
            tx2.Text = ""
            tx.Text = ""
        End If

    End Sub
    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        LoadPR(ComboBox1, ComboBox2, TextBox1, TextBox2, NumericUpDown1)
    End Sub
    Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
        LoadPR(ComboBox4, ComboBox3, TextBox6, TextBox5, NumericUpDown2)
    End Sub
    Private Sub ComboBox5_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox5.SelectedIndexChanged
        LoadPR(ComboBox6, ComboBox5, TextBox9, TextBox8, NumericUpDown3)
    End Sub
    Private Sub ComboBox7_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox7.SelectedIndexChanged
        LoadPR(ComboBox8, ComboBox7, TextBox12, TextBox11, NumericUpDown4)
    End Sub
    Private Sub ComboBox9_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox9.SelectedIndexChanged
        LoadPR(ComboBox10, ComboBox9, TextBox15, TextBox14, NumericUpDown5)
    End Sub
    Private Sub ComboBox11_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox11.SelectedIndexChanged
        LoadPR(ComboBox12, ComboBox11, TextBox18, TextBox17, NumericUpDown6)
    End Sub
    Private Sub ComboBox13_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox13.SelectedIndexChanged
        LoadPR(ComboBox14, ComboBox13, TextBox21, TextBox20, NumericUpDown7)
    End Sub

#End Region

#Region "数量变化"

    '数量变化
    Private Sub SLchange(ByVal nu As NumericUpDown, ByVal tx1 As TextBox, ByVal tx2 As TextBox)
        tx2.Text = (Val(tx1.Text) * nu.Value).ToString
    End Sub
    Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
        SLchange(NumericUpDown1, TextBox1, TextBox2)
    End Sub
    Private Sub NumericUpDown2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown2.ValueChanged
        SLchange(NumericUpDown2, TextBox6, TextBox5)
    End Sub
    Private Sub NumericUpDown3_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown3.ValueChanged
        SLchange(NumericUpDown3, TextBox9, TextBox8)
    End Sub
    Private Sub NumericUpDown4_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown4.ValueChanged
        SLchange(NumericUpDown4, TextBox12, TextBox11)
    End Sub
    Private Sub NumericUpDown5_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown5.ValueChanged
        SLchange(NumericUpDown5, TextBox15, TextBox14)
    End Sub
    Private Sub NumericUpDown6_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown6.ValueChanged
        SLchange(NumericUpDown6, TextBox18, TextBox17)
    End Sub
    Private Sub NumericUpDown7_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown7.ValueChanged
        SLchange(NumericUpDown7, TextBox21, TextBox20)
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        NumericUpDown1_ValueChanged(sender, e)
    End Sub
    Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged
        NumericUpDown2_ValueChanged(sender, e)
    End Sub
    Private Sub TextBox9_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox9.TextChanged
        NumericUpDown3_ValueChanged(sender, e)
    End Sub
    Private Sub TextBox12_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox12.TextChanged
        NumericUpDown4_ValueChanged(sender, e)
    End Sub
    Private Sub TextBox15_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox15.TextChanged
        NumericUpDown5_ValueChanged(sender, e)
    End Sub
    Private Sub TextBox18_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox18.TextChanged
        NumericUpDown6_ValueChanged(sender, e)
    End Sub
    Private Sub TextBox21_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox21.TextChanged
        NumericUpDown7_ValueChanged(sender, e)
    End Sub

#End Region

    Dim flag As Integer = 0

    '窗体加载
    Private Sub 批量入货_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '加载品牌
        Dim sqlstr As String = "select 品牌 from 品牌表"
        Dim ds As Data.DataSet = SQLtoDataSet(sqlstr)
        Dim ds_table As Data.DataTable = ds.Tables(0)
        If ds_table.Rows.Count > 0 Then
            For i = 0 To ds_table.Rows.Count - 1
                ComboBox1.Items.Add(ds_table.Rows(i).Item(0).ToString)
                ComboBox4.Items.Add(ds_table.Rows(i).Item(0).ToString)
                ComboBox6.Items.Add(ds_table.Rows(i).Item(0).ToString)
                ComboBox8.Items.Add(ds_table.Rows(i).Item(0).ToString)
                ComboBox10.Items.Add(ds_table.Rows(i).Item(0).ToString)
                ComboBox12.Items.Add(ds_table.Rows(i).Item(0).ToString)
                ComboBox14.Items.Add(ds_table.Rows(i).Item(0).ToString)
            Next
        End If

    End Sub

    '入货确定 待改
    Private Sub checkitout(ByVal cb1 As ComboBox, ByVal cb2 As ComboBox, ByVal nu As NumericUpDown, ByVal tx1 As TextBox, ByVal tx2 As TextBox, ByVal tx3 As TextBox)
        Dim PP As String = cb1.Text
        Dim XH As String = cb2.Text
        Dim SL As String = nu.Value.ToString
        Dim DJ As String = tx1.Text
        Dim ZE As String = tx2.Text
        Dim BZ As String = tx3.Text

        If cb1.Text = "请选择..." Or cb1.Text = "" Then
            MsgBox("请选择你要入货的品牌！")
            cb1.Focus()
            Exit Sub
        End If

        If cb2.Text = "请选择..." Or cb2.Text = "" Then
            MsgBox("请选择你要入货的型号！")
            cb2.Focus()
            Exit Sub
        End If

        If Not IsNumeric(DJ) Then
            MsgBox("你输入的入价单价不是一个正确的数字，请重新输入！")
            tx1.Focus()
            Exit Sub
        End If

        If Not IsNumeric(ZE) Then
            MsgBox("你输入的总价不是一个正确的数字，请重新输入！")
            tx2.Focus()
            Exit Sub
        End If

        flag += 1

    End Sub
    Private Sub addup(ByVal cb1 As ComboBox, ByVal cb2 As ComboBox, ByVal nu As NumericUpDown, ByVal tx1 As TextBox, ByVal tx2 As TextBox, ByVal tx3 As TextBox)
        Dim PP As String = cb1.Text
        Dim XH As String = cb2.Text
        Dim SL As String = nu.Value.ToString
        Dim DJ As String = tx1.Text
        Dim ZE As String = tx2.Text
        Dim BZ As String = tx3.Text

        Dim RQ As String = Now.ToString

        Dim sqlstr As String = ""
        sqlstr += "insert into 入货表(品牌,型号,数量,入货单价,合计总额,日期,备注1) values("
        sqlstr += "'" + PP + "',"
        sqlstr += "'" + XH + "',"
        sqlstr += "'" + SL + "',"
        sqlstr += "'" + DJ + "',"
        sqlstr += "'" + ZE + "',"
        sqlstr += "'" + RQ + "',"
        sqlstr += "'" + BZ + "'"
        sqlstr += ")"
        SQLupdata(sqlstr)

        cb1.SelectedIndex = -1
        cb2.SelectedIndex = -1
        nu.Value = 1
        tx1.Text = ""
        tx2.Text = ""
        tx3.Text = ""

    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        flag = 0

        Dim a1 As Integer = 1

        checkitout(ComboBox1, ComboBox2, NumericUpDown1, TextBox1, TextBox2, TextBox3) : If flag <> a1 Then Exit Sub

        If CheckBox1.Checked Then checkitout(ComboBox4, ComboBox3, NumericUpDown2, TextBox6, TextBox5, TextBox4) : a1 += 1 : If flag <> a1 Then Exit Sub

        If CheckBox2.Checked Then checkitout(ComboBox6, ComboBox5, NumericUpDown3, TextBox9, TextBox8, TextBox7) : a1 += 1 : If flag <> a1 Then Exit Sub

        If CheckBox3.Checked Then checkitout(ComboBox8, ComboBox7, NumericUpDown4, TextBox12, TextBox11, TextBox10) : a1 += 1 : If flag <> a1 Then Exit Sub

        If CheckBox4.Checked Then checkitout(ComboBox10, ComboBox9, NumericUpDown5, TextBox15, TextBox14, TextBox13) : a1 += 1 : If flag <> a1 Then Exit Sub

        If CheckBox5.Checked Then checkitout(ComboBox12, ComboBox11, NumericUpDown6, TextBox18, TextBox17, TextBox16) : a1 += 1 : If flag <> a1 Then Exit Sub

        If CheckBox6.Checked Then checkitout(ComboBox14, ComboBox13, NumericUpDown7, TextBox21, TextBox20, TextBox19) : a1 += 1 : If flag <> a1 Then Exit Sub


        '检测有误
        If flag <> a1 Then Exit Sub

        '检测无误

        addup(ComboBox1, ComboBox2, NumericUpDown1, TextBox1, TextBox2, TextBox3)

        If CheckBox1.Checked Then
            addup(ComboBox4, ComboBox3, NumericUpDown2, TextBox6, TextBox5, TextBox4)
        End If
        If CheckBox2.Checked Then
            addup(ComboBox6, ComboBox5, NumericUpDown3, TextBox9, TextBox8, TextBox7)
        End If
        If CheckBox3.Checked Then
            addup(ComboBox8, ComboBox7, NumericUpDown4, TextBox12, TextBox11, TextBox10)
        End If
        If CheckBox4.Checked Then
            addup(ComboBox10, ComboBox9, NumericUpDown5, TextBox15, TextBox14, TextBox13)
        End If
        If CheckBox5.Checked Then
            addup(ComboBox12, ComboBox11, NumericUpDown6, TextBox18, TextBox17, TextBox16)
        End If
        If CheckBox6.Checked Then
            addup(ComboBox14, ComboBox13, NumericUpDown7, TextBox21, TextBox20, TextBox19)
        End If

        If flag > 0 Then
            MsgBox("入货操作成功！  (" + flag.ToString + "行)")
        End If

    End Sub

End Class